package fm.web;

import fm.cache.ConfigCache;
import fm.entity.PublicNumber;
import fm.entity.WxUser;
import fm.exception.TokenInvalidException;
import fm.service.PublicNumberService;
import fm.service.UserService;
import fm.util.CommonUtils;
import fm.util.Constant;
import fm.util.SpringContextHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * @修改人：CM
 * @修改时间：2017/2/27 20:46
 */
@Component
public class CurrentRequest {
    @Autowired
    private HttpServletRequest request;
    @Autowired
    private HttpSession session;

    /**
     * 获取当前用户
     *
     * @return
     * @throws Exception
     */
    public WxUser getCurrentWechatUser() throws Exception {
        WxUser user = (WxUser) session.getAttribute(Constant.SESSION_LOGIN_USER);
        if (user == null) {
            //TODO 正式环境应抛出TOKEN超时异常
//            throw new TokenInvalidException("身份验证已经超时，请刷新页面或者重新登陆！");
            user = SpringContextHolder.getApplicationContext().getBean(UserService.class)
                    .getByOpenIdAndUuid(ConfigCache.getConfig("TEST_OPENID"), ConfigCache.getConfig("DEFAULT_UUID"));
            return user;
        } else {
            return user;
        }
    }

    /**
     * 获取当前会话中的公众号
     * session中服务号信息的注入有两个途径：
     * 1.portal平台用户登录的时候
     * 2.app 端客户端访问时带的uuid会指向公众号所拥有的app，如果有查询到公众号信息，会注入（不会注入公众号的安全信息）
     *
     * @return
     */
    public PublicNumber getCurrentPublicNumber() throws TokenInvalidException {
        PublicNumber pn = (PublicNumber) session.getAttribute(Constant.SESSION_PUBLIC_NUMBER);
        if (!CommonUtils.isEmpty(pn)) {
            return pn;
        } else {
            //TODO 正式环境应抛出TOKEN超时异常
            pn = SpringContextHolder.getApplicationContext().getBean(PublicNumberService.class)
                    .getByUuid(ConfigCache.getConfig("DEFAULT_UUID"));
            return pn;
//            throw new TokenInvalidException("身份验证已经超时，请刷新页面或者重新登陆！");
        }
    }


    public HttpServletRequest getRequest() {
        return request;
    }

    public HttpSession getSession() {
        return session;
    }

    public void addSessionAttribute(String key, Object val) throws TokenInvalidException {
        if (this.session != null) {
            this.session.setAttribute(key, val);
        } else {
            throw new TokenInvalidException("身份验证已经超时，请刷新页面或者重新登陆！");
        }
    }

    public void addRequestAttribute(String key, String val) throws TokenInvalidException {
        if (this.request != null) {
            this.request.setAttribute(key, val);
        } else {
            throw new TokenInvalidException("身份验证已经超时，请刷新页面或者重新登陆！");
        }
    }

    public Object getRequestAttribute(String key) throws TokenInvalidException {
        if (this.request != null) {
            return this.request.getAttribute(key);
        } else {
            throw new TokenInvalidException("身份验证已经超时，请刷新页面或者重新登陆！");
        }
    }

    public Object getSessionAttribute(String key) throws TokenInvalidException {
        if (this.session != null) {
            return this.session.getAttribute(key);
        } else {
            throw new TokenInvalidException("身份验证已经超时，请刷新页面或者重新登陆！");
        }
    }
}
